import { Request, valiMobile, errorToast, successToast } from '@/utils'
import md5 from 'md5'
import mselect from '@/components/mselect.vue'
let request = new Request()

export default {
	data () {
		return {
			storeId : 0,
			account : '',
			loadComplete : false,
			formData : {
				store_account : '',
				store_name : '',
				contact : '',
				mobile : '',
				service_telephone : '',
				profit_rate : '',
				province_id : '',
				city_id     : '',
				district_id : '',
				address : '',
				status : 1,
				enable : false
			},
			
			copyData : {},
			
			rules : {
				store_account : [
					{required : true, message : '请设置登录账号', trigger : 'blur'}
				],
				
				store_name : [
					{required : true, message : '请填写门店名称', trigger : 'blur'}
				]
			}
		}
	},
	
	watch : {
		'formData.enable' : function(value){
			this.$set(this.formData, 'status', value ? 0 : 1)
		}
	},
	
	methods : {
		submit : function(){
			this.$refs.form.validate(validate => {
				if(!validate){
					return
				}
				
				let formData = Object.assign({}, this.formData)
				
				formData.status = formData.enable ? 1 : 0
				delete formData.enable
				
				if(this.storeId > 0){
					formData.store_id = this.storeId
				}
				
				request.post('store/save', res => {
					if(res.success){
						successToast(res.msg, this)
						this.$router.go(-1)
					}else{
						errorToast(res.msg, this)
					}
				}, formData)
				
				
			})
		}
	},
	
	created : function(){
		let storeId = this.$route.params.storeId || ''
		if(storeId){ //编辑
			this.storeId = storeId
			request.get('store/getEditInfo', res => {
				if(res.success){
					let data = res.data
					
					for(let k in data){
						if(k in this.formData){
							this.formData[k] = data[k]
						}
					}
					
					this.formData.enable = data.status == 1 ? true : false
				}else{
					errorToast(res.msg, this)
				}
				
			}, {id : storeId})
		}else{
			/* this.$set(this.formData, 'status', 0)
			this.$set(this.formData, 'enable', true) */
		}
	},
	
	components : {
		mselect
	},
	activated () {
		if(!this.ISFIRST){
			this.ISFIRST = true
		}else{
			this.$refs.pagination.setInit(1)
			this.$refs.pagination.getPage()
		}
	}
}